<?php

namespace App\Http\Controllers\Client\Activity;

use App\Http\Controllers\ClientController;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ActivityCreateBeforeController extends ClientController
{

    /**
     * 发布霸王餐前的查询
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        try {
            $remaining = DB::table('lottery_remaining as lr')
                ->leftJoin('shop_type as st','lr.st_id','=','st.st_id')
                ->where('lr.we_id','=',$this->we_id)
                ->select('lr.lr_id','lr.we_id','lr.st_id','lr.remaining_times','st.name','st.participate_number','st.participate_time')
                ->get()
                ->toArray();
            $remaining = json_decode(json_encode($remaining), true);

            $goods = DB::table('lottery_goods as lg')
                ->leftJoin('shop', 'shop.shop_id', '=', 'lg.shop_id')
                ->leftJoin('dict_area as da','shop.district','=','da.id')
                ->leftJoin('lottery_remaining as lr',function($join){
                    $join->on('shop.shop_type','=','lr.st_id')->where('lr.we_id','=',$this->we_id);
                })
                ->leftJoin('shop_type as st','shop.shop_type','=','st.st_id')
                ->where('lg.creator', '=', $this->we_id)
                ->where('shop.verified','=',Data::DATA_DB_LAIFEI_SHOP_VERIFIED_SUCCESS)
                ->whereIn('shop.city',Data::$lotteryPermitCity)
                ->where('lg.is_delete', '=', 0)//未禁用的活动策略
                ->select('shop.shop_id', 'shop.shop_name', 'lg.goods_id', 'lg.goods_name','da.short_name','lr.remaining_times','st.name as type_name','st.participate_number','st.participate_time')
                ->get()
                ->toArray();
            $goods = json_decode(json_encode($goods), true);
            Log::debug('goods',$goods);
            $shops = [];
            foreach ($goods as $good) {
                if (!isset($shops[$good['shop_id']])) {
                    $shops[$good['shop_id']] = [
                        'shop_id' => $good['shop_id'],
                        'shop_name' => $good['shop_name'],
                        'type_name' => $good['type_name'],
                        'participate_number' => $good['participate_number'],
                        'participate_time' => $good['participate_time'],
                        'remaining_times' => $good['remaining_times'] ?? 0,
                        'goods' => [[
                            'goods_id' => $good['goods_id'],
                            'goods_name' => $good['goods_name'],
                            'short_name' => $good['short_name'],
                        ]],
                    ];
                } else {
                    $shops[$good['shop_id']]['goods'][] = [
                        'goods_id' => $good['goods_id'],
                        'goods_name' => $good['goods_name'],
                        'short_name' => $good['short_name'],
                    ];
                }
            }

            return $this->setReturnJson(0, 'success',  ['shops'=>array_values($shops),'remaining'=>$remaining]);
        } catch (\Exception $e) {
            Log::error(ActivityCreateBeforeController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败');
        }
    }

}
